package com.atguigu.gulimall.product.controller;

import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrAttrgroupRelationService;
import com.atguigu.gulimall.product.service.AttrGroupService;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.AttrAttrGroupRelationVo;
import com.atguigu.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * 属性分组
 *
 * @author liuyong
 * @email sunlightcs@gmail.com
 * @date 2020-07-06 14:12:57
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrAttrgroupRelationService relationService;

    /**
     * 列表
     */
//    @RequestMapping("/list")
//    public R list(@RequestParam Map<String, Object> params){
//        PageUtils page = attrGroupService.queryPage(params);
//        return R.ok().put("page", page);
//    }


    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params, @PathVariable Long catelogId) {
        PageUtils page = attrGroupService.queryPageByCategoryId(params, catelogId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        long catelogId = attrGroup.getCatelogId();
        //根据组id查出组的路径
        Long[] catelogPath = categoryService.getCatelogPath(catelogId);
        attrGroup.setCatelogPath(catelogPath);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);
        return R.ok();
    }

    /**
     * 分组关联属性列表
     *
     * @return
     */
    @RequestMapping("/{attrGroupId}/attr/relation")
    public R attrAttrGrRelation(@PathVariable("attrGroupId") Long attrGroupId) {
        List<AttrEntity> attrEntities = attrGroupService.attrAttrGrRelation(attrGroupId);
        return R.ok().put("data", attrEntities);
    }

    /**
     * 属性组关联基本属性
     *
     * @param list
     * @return
     */
    @PostMapping("/attr/relation")
    public R attrGroupAttrRelation(@RequestBody List<AttrAttrGroupRelationVo> list) {
        boolean save = relationService.saveBatchArrtAttrGroupRelation(list);
        return save == true ? R.ok() : R.error(400, "添加错误");
    }

    /**
     * 属性组未关联的属性
     *
     * @param attrGroupId
     * @return
     */
    @GetMapping(value = "/{attrGroupId}/noattr/relation")
    public R noattrRlation(@PathVariable("attrGroupId") Long attrGroupId, @RequestParam Map<String, Object> params) {
        PageUtils page = attrGroupService.attrAttrGroupNoRelation(attrGroupId, params);
        return R.ok().put("page", page);
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody List<Long> attrGroupIds) {
        attrGroupService.removeGroup(attrGroupIds);
        return R.ok();
    }

    /**
     * 获取分类下所有分组&关联属性
     *
     * @param catelogId
     * @return
     */
    @GetMapping("/{catelogId}/withattr")
    public R getSpuAttrInfo(@PathVariable("catelogId") Long catelogId) {
        List<AttrGroupWithAttrsVo> attrsVos = attrGroupService.getAttrGroupByCatelogId(catelogId);

        return R.ok().put("data", attrsVos);
    }

    /**
     * 删除属性组所关联的属性
     *
     * @param relationVo
     * @return
     */
    @RequestMapping("/attr/relation/delete")
    public R attrRelationDelete(@RequestBody AttrAttrGroupRelationVo[] relationVo) {
        attrGroupService.deleteAttrRelation(relationVo);
        return R.ok();
    }
}
